//
//  TestQuitAlertView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/6.
//

#import "TestQuitAlertView.h"

@implementation TestQuitAlertView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = UIColorHex_(0x000000,0.4);
    __weak typeof(self)self_weak_ = self;
    NSString *title = @"当前答题进度将丢失，确认退出吗？";
    NSString *highlightedText = @"丢失";
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:title];
    // 设置默认文本属性
    NSDictionary *defaultAttributes = @{
        NSForegroundColorAttributeName:KFontBlackColor,
        NSFontAttributeName:JPUIFontPingFangSCRegular(16)
    };
    [attributedText setAttributes:defaultAttributes range:NSMakeRange(0, attributedText.length)];
    // 设置高亮文本属性
    NSDictionary *highlightAttributes = @{
        NSForegroundColorAttributeName:UIColorHex(0xFE4A4B),
        NSFontAttributeName:JPUIFontPingFangSCRegular(16)
    };
    NSRange highlightedRange = [title rangeOfString:highlightedText];
    if (highlightedRange.location != NSNotFound) {
        [attributedText setAttributes:highlightAttributes range:highlightedRange];
    }
    CGFloat h = [self getStringHeightWithText:title font:JPUIFontPingFangSCRegular(16) viewWidth:210];
    if(h < 44){
        h = 44;
    }
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.layer.cornerRadius = 10;
    bgView.layer.masksToBounds = YES;
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@280);
        make.centerX.mas_equalTo(self_weak_);
        make.height.mas_equalTo(118+h);
        make.centerY.mas_equalTo(self_weak_);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.numberOfLines = 0;
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.attributedText = attributedText;
    [bgView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(@210);
        make.top.mas_equalTo(@35);
        make.centerX.mas_equalTo(bgView);
    }];
    
    CGFloat btn_w = 278/2.0;
    UIButton *cancelBtn = [[UIButton alloc]init];
    [cancelBtn setTitle:@"继续考试" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
        make.width.mas_equalTo(btn_w);
    }];
    
    UIButton *quitBtn = [[UIButton alloc]init];
    [quitBtn setTitle:@"退出" forState:UIControlStateNormal];
    [quitBtn setTitleColor:KFontGrayColor forState:UIControlStateNormal];
    quitBtn.titleLabel.font = JPUIFontPingFangSCRegular(16);
    [quitBtn addTarget:self action:@selector(quitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:quitBtn];
    [quitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@48);
        make.width.mas_equalTo(btn_w);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = KLineColor;
    [bgView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@-48.5);
        make.height.mas_equalTo(0.5);
        make.left.right.mas_equalTo(@0);
    }];
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = KLineColor;
    [bgView addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@0);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(@48);
        make.centerX.mas_equalTo(bgView);
    }];
    
}


-(void)cancelBtnClick{
    if(_quitBlock){
        _quitBlock(0);
    }
}

-(void)quitBtnClick{
    if(_quitBlock){
        _quitBlock(1);
    }
}

@end
